/*
 * Copyright 2010-2012 Susanta Tewari. <freecode4susant@users.sourceforge.net>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package javautil.graphics;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;

import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JComponent;

/**
 *
 * @author stewari1
 */
public class GraphicsUtil {

    /** Field description */
    public static final String IMAGE_FORMAT_JPEG = "jpeg";

    /** Field description */
    public static final String IMAGE_FORMAT_PNG = "png";

    /**
     *
     * @param words
     * @param fontMetrics
     * @return
     */
    public static int[] getPixelWidth(String[] words, FontMetrics fontMetrics) {

        int[] temp = new int[words.length];

        for (int i = 0; i < words.length; i++) {

            temp[i] = findWordPixelWidth(words[i], fontMetrics);
        }

        return temp;
    }

    /**
     *
     * @param word
     * @param fontMetrics
     * @return
     */
    public static int findWordPixelWidth(String word, FontMetrics fontMetrics) {
        return (fontMetrics.stringWidth(word));
    }

    public static void saveImage(File file, String format, JComponent comp, int width, int height) {

        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
        Graphics2D g2       = image.createGraphics();

        comp.paint(g2);
        g2.dispose();

        try {
            ImageIO.write(image, format, file);
        } catch (IOException ioe) {
            System.err.println(ioe.getMessage());
        }
    }

    public static void saveImage(File file, String format, int width, int height,
                                 ChartPanel chartPanel) {

        JFreeChart chart                 = chartPanel.getChart();
        ChartRenderingInfo renderingInfo = chartPanel.getChartRenderingInfo();

        try {

            if (format.equals(GraphicsUtil.IMAGE_FORMAT_JPEG)) {
                ChartUtilities.saveChartAsJPEG(file, chart, width, height, renderingInfo);
            }

            if (format.equals(GraphicsUtil.IMAGE_FORMAT_PNG)) {
                ChartUtilities.saveChartAsPNG(file, chart, width, height, renderingInfo);
            }

        } catch (IOException iOException) {
            iOException.printStackTrace();
        }
    }
}
